﻿/*  
 *  IIS LogFile Analyser, Copyright (C) 2013. http://www.iis-analyser.com
 *  --
 *  This program is free software, provided "AS IS" WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE
 * 
 *  File Author: Ian Quigley,    2013-02-23
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataStore;
using System.Configuration;
using System.Text;
using System.Reflection;
using Web.DisplayView;

namespace Web
{
    public partial class ViewList : BasePage
    {
        protected void Page_Load( object sender, EventArgs e )
        {
            if (IsPostBack)
                return;
            
            PopulateDisplayViews();
            PopulateFilterSelection();

            var id = QueryParamLong( "edit" );
            if (id != 0)
            {
                var view = Data.View( id );
                if (view != null)
                {
                    hidID.Value = view.Id.ToString();
                    txtName.Text = view.Name;
                    txtSQL.Text = view.Sql;
                    hidFilters.Value = view.Filters;

                    StringBuilder html = new StringBuilder();
                    foreach (string s in view.Filters.Split(' ').Where(s => !string.IsNullOrWhiteSpace(s)))
                        html.Append("<div class='filterItem' data-value='" + s + "'>" + s + "<a href='#' onclick='filterDelete(this);'>X</a></div>");

                    listFilters.InnerHtml = html.ToString();

                    DropdownSelect( drpViews, view.DisplayView );
                    chkInMenu.Checked = view.InMenu;
                    btnUpdate.Text = "Update";
                }
            }

            id = QueryParamLong( "delete" );
            if (id != 0)
            {
                Data.DeleteView( id );
            }


            UpdateDisplay();
        }

        
        
        protected void btnUpdate_Click( object sender, EventArgs e )
        {
            long id;
            ViewRecord view;

            if (long.TryParse( hidID.Value, out id ))
                view = Data.View( id );
            else
                view = new ViewRecord();

            view.Name = txtName.Text;
            view.Sql = txtSQL.Text;
            view.Filters = hidFilters.Value.Trim();
            
            view.DisplayView = drpViews.SelectedItem.Text;
            view.InMenu = chkInMenu.Checked;
            view.Save();

            Response.Redirect( "viewlist.aspx" );
        }

        
        private void UpdateDisplay()
        {
            StringBuilder html = new StringBuilder();

            html.Append( "<table cellspacing='0'><thead><tr><td>View</td><td>Actions</td></tr></thead>" );
            foreach (var view in Data.Views())
                html.AppendFormat( "<tr><td><a href=\"view.aspx?id={0}\">View {1}</a></td><td><a href=\"viewlist.aspx?edit={0}\">Edit</a>, <a href=\"viewlist.aspx?delete={0}\">Delete</a><br/>", view.Id, view.Name );
            html.Append( "</table>" );

            litList.Text = html.ToString();            
        }

        private void PopulateDisplayViews()
        {
            drpViews.DataSource = ViewFactory.Names;
            drpViews.DataBind();
        }

        private void PopulateFilterSelection()
        {
            StringBuilder html = new StringBuilder();
            html.Append( "<select id='filters'>" );
            foreach (var filter in Filter.FilterFactory.Types)
                html.AppendFormat( "<option value='{0}'>{1}</option>", filter.Value.Name, filter.Key);

            html.Append( "</select>" );

            litFilterSelection.Text = html.ToString();
        }
     
    }
}